package com.teach.servlet.admin;

import com.alibaba.fastjson.JSON;
import com.teach.R;
import com.teach.dao.MedalInfoDAO;
import com.teach.dao.impl.MedalInfoDAOImpl;
import com.teach.dto.MedalSearch;
import com.teach.dto.Search;
import com.teach.service.MedalService;
import com.teach.service.impl.MedalServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/medal-page.let")
public class MedalPageServlet extends HttpServlet {

    private MedalService service = new MedalServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MedalSearch search = new MedalSearch();
        try{
            search.setCurrent(Integer.valueOf(request.getParameter("current")));
        }catch (Exception ex){}
        try{
            search.setRowMax(Integer.valueOf(request.getParameter("rowMax")));
        }catch (Exception ex){}

        try{
            search.setSeId(Integer.valueOf(request.getParameter("seId")));
        }catch (Exception ex){}

        search.setKey(request.getParameter("key"));

        response.reset();
        response.setContentType("application/json;charset=UTF-8");
        try {
//            List<MedalInfo> list = dao.select(0, 999);
            response.getWriter().print(JSON.toJSONString(service.page(search)));
        } catch (Exception ex) {
            response.getWriter().print(JSON.toJSONString(R.error().setMessage(ex.getMessage())));
            ex.printStackTrace();
        }
        response.flushBuffer();
    }
}

